Imports System.Xml
Imports System.Xml.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO
Imports System.Text
Public Class CustomSerializer

    Private Shared strfilename As String

    Private Shared strFilePath As String = HttpContext.Current.Server.MapPath("") & "\bin\temp\"
    Shared Function FromXML(ByVal strxml As String) As StreamReader
        'strxml = "<?xml version=""1.0"" encoding=""utf-8""?>" & strxml

        WriteToFile(strxml)
        Dim objstreamreader As New StreamReader(strFilePath & strfilename)
        Return objstreamreader

        'Dim stream As New MemoryStream
        'Dim str() As Byte = Encoding.UTF8.GetBytes(strxml)
        'stream.Write(str, 0, str.Length)
        'Dim objstreamreader As New StreamReader(stream)
        'Dim objstreamreader As New StreamReader("C:\Documents and Settings\adwara\My Documents\Visual Studio 2005\Projects\AJAXEnabledWebApplication2\bin\sample.xml")
    End Function

    Shared Sub DeleteFile(ByVal strFileName As String)
        Try
            Dim objfile As New FileInfo(strFileName)
            objfile.Delete()
        Catch
        End Try

    End Sub

    Shared Function FromXML(ByVal strxml As String, ByVal objType As Type) As Object

        If Not strxml.Contains("xml version") Then
            strxml = "<?xml version=""1.0""?>" & strxml
        End If


        'strxml = "<GetAdditionalCardWSRequest><IdentityBlock xmlns=""http://www.americanexpress.com/schemas/RequestBlock""><ChannelID xmlns="""" /><LUNbr xmlns="""" /><AuditID xmlns="""" /><ApplicationID xmlns="""">400</ApplicationID><ClientMessageID xmlns=""""/></IdentityBlock><ServiceBlock xmlns=""http://www.americanexpress.com/schemas/RequestBlock""><FuncNm xmlns="""" /><FuncVersNbr xmlns="""" /><ServiceNm xmlns="""" /><SrvcMajVersionNbr xmlns="""" /><SrvcMinVersionNbr xmlns="""" /></ServiceBlock><RequestParameters xmlns=""http://www.americanexpress.com/schemas/RewardsEnrollmentEligibilityProcessor/GetAdditionalCardsWSRequest""><IdentifierParameters xmlns=""""><BusObjId /><BusObjTypeCd xmlns=""http://www.americanexpress.com/schemas/RewardsEnrollmentEligibilityProcessor/GetAdditionalCardsWSRequest"" /></IdentifierParameters><AddtionalCard><BusObjId xmlns="""" /></AddtionalCard><EnrollInfoGrp><PorfoCd xmlns="""" /><EnrollInd xmlns="""" /></EnrollInfoGrp><EnrollInd xmlns="""" /><ValidateGrp><CMNbr xmlns="""" /><SocSecNbr xmlns="""" /><PrimCdInd xmlns="""" /></ValidateGrp></RequestParameters></GetAdditionalCardWSRequest>"
        'strxml = "<GetAdditionalCardWSRequest><IdentityBlock xmlns=""http://www.americanexpress.com/schemas/RequestBlock""><ChannelID xmlns="""" /><LUNbr xmlns="""" /><AuditID xmlns="""" /><ApplicationID xmlns="""">400</ApplicationID><ClientMessageID xmlns=""""/></IdentityBlock><ServiceBlock xmlns=""http://www.americanexpress.com/schemas/RequestBlock""><FuncNm xmlns="""" /><FuncVersNbr xmlns="""" /><ServiceNm xmlns="""" /><SrvcMajVersionNbr xmlns="""" /><SrvcMinVersionNbr xmlns="""" /></ServiceBlock><RequestParameters xmlns=""http://www.americanexpress.com/schemas/RewardsEnrollmentEligibilityProcessor/GetAdditionalCardsWSRequest""><IdentifierParameters xmlns=""""><BusObjId /><BusObjTypeCd xmlns=""http://www.americanexpress.com/schemas/RewardsEnrollmentEligibilityProcessor/GetAdditionalCardsWSRequest"" /></IdentifierParameters><AddtionalCard><BusObjId xmlns="""" /><BusObjTypeCd /></AddtionalCard><EnrollInfoGrp><PorfoCd xmlns="""" /><EnrollInd xmlns="""" /></EnrollInfoGrp><EnrollInd xmlns="""" /><ValidateGrp><CMNbr xmlns="""" /><SocSecNbr xmlns="""" /><PrimCdInd xmlns="""" /></ValidateGrp></RequestParameters></GetAdditionalCardWSRequest>"
        'strxml = "<GetAdditionalCardWSRequest xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><IdentityBlock xmlns=""http://www.americanexpress.com/schemas/RequestBlock""><ApplicationID xmlns="""">400</ApplicationID></IdentityBlock><RequestParameters xmlns=""http://www.americanexpress.com/schemas/RewardsEnrollmentEligibilityProcessor/GetAdditionalCardsWSRequest""><AddtionalCard><BusObjId>234832094</BusObjId><BusObjTypeCd>CM NUMBER</BusObjTypeCd></AddtionalCard></RequestParameters></GetAdditionalCardWSRequest>"
        'strxml = strxml.Replace("xmlns=""""", "")
        'Try

        'Dim xmldom As New XmlDocument
        'xmldom.LoadXml(strxml)

        'RemoveAttributes(xmldom.FirstChild)


        'strxml = xmldom.InnerXml
        WriteToFile(strxml)


        Dim objstreamreader As New StreamReader(strFilePath & strfilename)
        'Dim objstreamreader As New StreamReader("C:\Documents and Settings\adwara\My Documents\Visual Studio 2005\Projects\AJAXEnabledWebApplication2\bin\sample.xml")

        Dim ser As XmlSerializer
        ser = New XmlSerializer(objType)

        Dim obj As Object
        obj = ser.Deserialize(objstreamreader)

        objstreamreader.Close()
        DeleteFile(strFilePath & strfilename)
        Return obj
        'Catch ex As Exception

        'End Try

        'Dim ser As XmlSerializer
        'ser = New XmlSerializer(objType)
        'Dim stringreader As StringReader
        'stringreader = New StringReader(strxml)
        'Dim xmlreader As XmlTextReader
        'xmlreader = New XmlTextReader(stringreader)
        'Dim obj As Object
        'Dim reader As New FileStream(Environment.CurrentDirectory & "\bin\sample.xml", FileMode.Open)
        'obj = ser.Deserialize(stringreader)
        'xmlreader.Close()
        'stringreader.Close()
        'reader.Close()


    End Function


    Shared Sub Cleanup()
        DeleteFile(strFilePath & strfilename)
    End Sub

    Shared Sub RemoveAttributes(ByVal xmlnode As XmlNode)
        Dim xmlchildnode As XmlNode


        For Each xmlchildnode In xmlnode.ChildNodes
           

            If xmlchildnode.GetType.Name <> "XmlText" Then
                Dim objxmlattribcol As XmlAttributeCollection

                objxmlattribcol = xmlchildnode.Attributes
                objxmlattribcol.RemoveAll()
            End If


            RemoveAttributes(xmlchildnode)

        Next
    End Sub

    Shared Function ToXML(ByVal obj As Object, ByVal objType As Type) As String

        Dim ser As XmlSerializer
        ser = New XmlSerializer(obj.GetType) ', CustomSerializer.TargetNamespace)

        Dim memstream As MemoryStream
        memstream = New MemoryStream
        'Dim xmlwriter As XmlTextWriter
        'xmlwriter = New XmlTextWriter(memstream, Encoding.UTF8)
        'xmlwriter.Namespaces = True
        ser.Serialize(memstream, obj) ', CustomSerializer.GetNameSpaces)
        'xmlwriter.Close()
        memstream.Close()


        Dim strxml As String
        strxml = Encoding.UTF8.GetString(memstream.GetBuffer())



        'strxml = strxml.Substring(strxml.IndexOf(Convert.ToChar(60)))
        'strxml = strxml.Substring(0, (strxml.LastIndexOf(Convert.ToChar(62)) + 1))

        'Dim streamwriter As New StreamWriter(Environment.CurrentDirectory & "\bin\sendxml.xml", False)
        'streamwriter.Write(strxml)
        'streamwriter.Flush()
        'streamwriter.Close()


        Return strxml

    End Function


    Shared Sub WriteToFile(ByVal strXML As String)

        strfilename = Environment.TickCount.ToString().GetHashCode().ToString("x")
        Dim objstreamwriter As New StreamWriter(strFilePath & strfilename, False)
        objstreamwriter.Write(strXML)
        objstreamwriter.Flush()
        objstreamwriter.Close()

    End Sub

    Shared Function GetNameSpaces() As XmlSerializerNamespaces
        Dim ns As XmlSerializerNamespaces
        ns = New XmlSerializerNamespaces
        ns.Add("xs", "http://www.w3.org/2001/XMLSchema")
        ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance")
        Return ns

    End Function


    Shared Function TargetNamespace() As String
        Return "http://www.w3.org/2001/XMLSchema"
    End Function


End Class




Public Class OrderedItem
    '<XmlElement(Namespace:="http://www.cpandl.com")> _
    Public ItemName As String

    '<XmlElement(Namespace:="http://www.cpandl.com")> _
    Public Description As String

    '<XmlElement(Namespace:="http://www.cohowinery.com")> _
    Public UnitPrice As Decimal

    '<XmlElement(Namespace:="http://www.cpandl.com")> _
    Public Quantity As Integer

    '<XmlElement(Namespace:="http://www.cohowinery.com")> _
    Public LineTotal As Decimal

    'A custom method used to calculate price per item.
    Public Sub Calculate()
        LineTotal = UnitPrice * Quantity
    End Sub
End Class